package programmercarl.动态规划.C11;

public class BagProblem1 {
    public static void main(String[] args) {
        int[] weight = {1, 3, 4};
        int[] value = {15, 20, 30};
        int bagSize = 4;
        testWeightBagProblem(weight, value, bagSize);
    }


    private static void testWeightBagProblem(int[] weight, int[] value, int bagSize) {
        int num = weight.length;
        int[][] dp = new int[num][bagSize + 1];
        for (int j = weight[0]; j <= bagSize; j++) {
            dp[0][j] = value[0];
        }

        for (int i = 0; i < num; i++) {
            for (int j = 1; j <= bagSize; j++) {
                if (weight[i] < j) {
                    dp[i][j] = Math.max(dp[i][j], dp[i][j - weight[i]] + value[i]);
                }else {
                    dp[i][j] = dp[i - 1][j];//放不下第i个物品，那么只能尝试放。
                }
            }
        }
    }
}
